//
//  BackgroundViewController.m
//  namefight
//
//  Created by nasa on 11-4-6.
//  Copyright 2011年 nasawz.com. All rights reserved.
//

#import "BackgroundViewController.h"

@interface BackgroundViewController (PrivateMethods)
- (void)hideGradientBackground:(UIView*)theView;
@end
@implementation BackgroundViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
		self.title = @"背景";
        
		self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"背景" image:[UIImage imageNamed:@"icon_background.png"] tag:0] autorelease];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView
{
    [super loadView];
    UIImageView * bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paper_bg.png"]];
    [self.view addSubview:bgView];
    TT_RELEASE_SAFELY(bgView);
    
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
    
    _webView = [[UIWebView alloc] initWithFrame:rect];
    _webView.delegate = self;
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth
    | UIViewAutoresizingFlexibleHeight;
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
    
    
    UIImageView * paper_buttom = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paper_buttom.png"]];
    paper_buttom.frame =CGRectMake(0, 360, 318, 8);
    [self.view addSubview:paper_buttom];
    TT_RELEASE_SAFELY(paper_buttom);
    
    NSString * path = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"html"];
    NSData *htmlData = [NSData dataWithContentsOfFile:path];
    [_webView setBackgroundColor:[UIColor clearColor]];
    [self hideGradientBackground:_webView];
    
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
	NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:resourcePath isDirectory:YES];
    
    
    [_webView 	loadData:htmlData 
               MIMEType:@"text/html"	
       textEncodingName:@"UTF-8" 
                baseURL:baseURL];
    
}

- (void) hideGradientBackground:(UIView*)theView
{
    for (UIView * subview in theView.subviews)
    {
        if ([subview isKindOfClass:[UIImageView class]])
            subview.hidden = YES;
        
        [self hideGradientBackground:subview];
    }
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
 navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView*)webView {
}
- (void)webViewDidFinishLoad:(UIWebView*)webView {
    //self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
}
@end
